
//
// 130724 - AH - (Paris) Created.
//

package org.bisonservice;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
@SuppressWarnings("serial")
public class BisonServiceServlet extends HttpServlet
{
  private static final String CONNECTION_URL=
    "jdbc:google:rdbms://ahaspaces:ahaxdb/gim";
		  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public BisonServiceServlet(){}
  
  /**
   * <p>
   *   
   * </p>
   */
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
  {
    perform(req, resp);
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
  {
	  perform(req, resp);
  }
  
  private void perform(HttpServletRequest req, HttpServletResponse resp)
    throws IOException
  {
    resp.setContentType("text/plain");
    PrintWriter pw=resp.getWriter();

    Connection c=null;
    try
    {
      c=DriverManager.getConnection(CONNECTION_URL);
      BisonAccess ba=new BisonAccess(c);
	
      BisonReceived br=parseRequest(req);
      System.err.println("bison-server: perform: br:");
      System.err.println(br);
      if (br==null)
      {
        performNoPar(pw, ba);
	  }
	  else
	  {
	    String oper=br.getOperation();
	    if (oper.equals("register"))
	    {
          performRegister(pw, ba, br);
	    }
	    else
	    {
          throw new IOException("uknown operation: "+oper);
	    }
	  }
    }
	catch (SQLException sqlx)
    {
      IOException iox=new IOException();
      iox.initCause(sqlx);
      throw iox;
    }
    finally
    {
      if (c!=null) try { c.close(); } catch (SQLException sqlx){}
    }
  }
  
  private void performRegister(PrintWriter pw, BisonAccess ba, BisonReceived br)
    throws IOException
  {
    ba.store(br);
    String reply=br.toLocation();
    pw.println(reply);
  }
  
  private void performNoPar(PrintWriter pw, BisonAccess ba) throws IOException
  {
    pw.println("Socrator Bison Service");
	        
    String[] bisons=ba.getBisons();
	      
    int n=bisons.length;
    if (n==0)
    {
      pw.println("No bisons registered.");
    }
    else
    {
      pw.println("Bisons:");
      pw.println("-------");
      for (int i=0; i<n; i++)
      {
        pw.println("'"+bisons[i]+"'");
      }
    }
	      
	pw.flush();
  }
  
  private BisonReceived parseRequest(HttpServletRequest req)
  {
    if (req.getParameterMap().isEmpty()) return null;
    return new BisonReceived(req);
  }
  
}
